//
//  YFEditProfileContentView.m
//  project
//
//  Created by Winny Zhang on 2019/3/28.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFEditProfileContentView.h"
#import "YFSelectedTagModel.h"
#import "LEEAlert.h"
#import "HJCityListVC.h"

@interface YFEditProfileContentView()

/**
 背景 View
 */
@property (weak, nonatomic) IBOutlet UIView *contentView;
/**
 图像外部圆环 View
 */
@property (weak, nonatomic) IBOutlet UIImageView *avatarCircleView;

/**
 头像 imageView
 */
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
/**
 昵称 输入框
 */
@property (weak, nonatomic) IBOutlet UITextField *nicknameTextField;
/**
性别 Label
 */
@property (weak, nonatomic) IBOutlet UILabel *genderLabel;
/**
 年龄输入框
 */
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
/**
 选择城市 按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *cityButton;
/**
 选择情感状态 按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *emotionButton;
/**
 添加标签按钮
 */
@property (weak, nonatomic) IBOutlet UIButton *tagButton;
/**
 签名输入框
 */
@property (weak, nonatomic) IBOutlet UITextField *signatureTextfield;



@end

@implementation YFEditProfileContentView

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupViews];
}


- (void)setupViews {
    
    [self.contentView rounded:(UIRectCornerAllCorners) radius:10];
    
    [self.avatarCircleView rounded:(UIRectCornerAllCorners) radius:self.avatarCircleView.height * 0.5];
    
    [self.avatarImageView rounded:(UIRectCornerAllCorners) radius:self.avatarImageView.height * 0.5];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        // 手势点击方法
        LKLog(@"点击手势");
    }];
    [self.avatarImageView addGestureRecognizer:tap];
    self.avatarImageView.userInteractionEnabled = YES;
    
    @weakify(self)
    
    // 昵称
    [[self.nicknameTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        if (x.length > 0) {
            [self updateParamsWithKey:@"nickName" value:x];
        }
    }];
    
    // 年龄
    [[self.ageTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        if (x.length > 0) {
            [self updateParamsWithKey:@"age" value:x];
        }
    }];
    
    // 选择城市
    [[self.cityButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self selectCities];
    }];
    
    // 选择情感
    [[self.emotionButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self selectEmotion];
    }];
    
    // 选择标签
    [[self.tagButton rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        LKLog(@"选择标签");
        @strongify(self)
        if (self.selectTagAction) {
            self.selectTagAction();
        }
    }];
    
    // 签名
    [[self.signatureTextfield rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        @strongify(self)
        if (x.length > 0) {
            [self updateParamsWithKey:@"intro" value:x];
        }
    }];
}

#pragma mark - setter
- (void)setTags:(NSArray<YFUserInfoTagModel *> *)tags {
    _tags = tags;
    NSString *tagsString = @"";
    if (tags.count > 0) {
        if (tags.count == 1) {
            tagsString = tags[0].name;
        } else {
            for (int i = 0; i < tags.count; i++) {
                if (i == 0) {
                    tagsString = tags[i].name;
                }  else {
                    tagsString = [[tagsString stringByAppendingString:@"/"] stringByAppendingString:tags[i].name];
                }
            }
        }
        [self.tagButton setTitle:tagsString forState:(UIControlStateNormal)];
        [self.tagButton setTitleColor:[UIColor withRGB:51] forState:(UIControlStateNormal)];
        
    } else {
        [self.tagButton setTitle:@"添加标签" forState:(UIControlStateNormal)];
        [self.tagButton setTitleColor:[UIColor withRGB:199] forState:(UIControlStateNormal)];
    }
    
    // 更新用户信息
    [self updateParamsWithKey:@"tags" value:[tags yy_modelToJSONString]];
}

- (void)setUserInfo:(HJUserModel *)userInfo {
    _userInfo = userInfo;
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:userInfo.avatar] placeholderImage:nil];
    self.nicknameTextField.text = userInfo.nickName;
    self.genderLabel.text = userInfo.sex.integerValue == 2 ? @"女" : @"男";
    self.ageTextField.text = userInfo.age;
    NSString *cityName = userInfo.city.cityName.length > 0 ? userInfo.city.cityName : @"请选择城市";
    UIColor *cityButtonColor = userInfo.city.cityName.length > 0 ? [UIColor withRGB:51] : [UIColor withRGB:199];
    [self.cityButton setTitle:cityName forState:(UIControlStateNormal)];
    [self.cityButton setTitleColor:cityButtonColor forState:(UIControlStateNormal)];
    NSString *emotionLabel = userInfo.emotion.length > 0 ? userInfo.emotion : @"请选择情感状态";
    UIColor *emotionColor = userInfo.emotion.length > 0 ? [UIColor withRGB:51] : [UIColor withRGB:199];
    [self.emotionButton setTitle:emotionLabel forState:(UIControlStateNormal)];
    [self.emotionButton setTitleColor:emotionColor forState:(UIControlStateNormal)];
    self.signatureTextfield.text = userInfo.intro;
    
    [self updateParamsWithKey:@"age" value:userInfo.age];
    [self updateParamsWithKey:@"avatar" value:userInfo.avatar];
    [self updateParamsWithKey:@"cityOid" value:userInfo.city.cityOid];
    [self updateParamsWithKey:@"emotion" value:userInfo.emotion];
    [self updateParamsWithKey:@"intro" value:userInfo.intro];
    [self updateParamsWithKey:@"nickName" value:userInfo.nickName];
    [self updateParamsWithKey:@"sex" value:userInfo.sex];
    [self updateParamsWithKey:@"userId" value:userInfo.userId];
    
    LKLog(@"用户资料: %@", self.params);
    
    // 设置标签
    self.tags = userInfo.tagModels;
    
    self.backupParams = self.params.copy;
}

#pragma mark - 选择城市列表
- (void)selectCities {
    HJCityListVC *vc = [HJCityListVC new];
    
    @weakify(self)
    vc.cityBlock = ^(NSDictionary * _Nonnull cityDic) {
        @strongify(self)
        NSString *cityName = cityDic[@"cityName"];
        [self.cityButton setTitle:cityName forState:(UIControlStateNormal)];
        [self.cityButton setTitleColor:[UIColor withRGB:51] forState:(UIControlStateNormal)];
        
        NSString *cityOid = cityDic[@"cityOid"];
        [self updateParamsWithKey:@"cityOid" value:cityOid];
        
    };
    [[UIViewController currentViewController].navigationController pushViewController:vc animated:YES];
}

#pragma mark - 显示选择情感的ActionSheet
- (void)selectEmotion {
    
    @weakify(self)
    [LEEAlert actionsheet].config
    .LeeAddAction(^(LEEAction *action) {
        
        action.title = @"情感状态";
        action.titleColor = [UIColor withRGB:51];
        action.font = [UIFont fontWithName:PingFangSC_Medium size:16];
        action.backgroundColor = [UIColor whiteColor];
        action.backgroundHighlightColor = [UIColor whiteColor];
        
    })
    .LeeAddAction(^(LEEAction *action) {
        
        @strongify(self)
        action.title = @"单身汪";
        action.titleColor = [UIColor withRGB:51];
        action.font = [UIFont fontWithName:PingFangSC_Regular size:16];
        action.backgroundColor = [UIColor whiteColor];
        
        @weakify(self)
        action.clickBlock = ^{
            
            @strongify(self)
            [self.emotionButton setTitle:@"单身汪" forState:(UIControlStateNormal)];
            [self.emotionButton setTitleColor:[UIColor withRGB:51] forState:(UIControlStateNormal)];
            [self updateParamsWithKey:@"emotion" value:@"单身汪"];
            
        };
    })
    .LeeAddAction(^(LEEAction *action) {
        
        @strongify(self)
        action.title = @"有人陪";
        action.titleColor = [UIColor withRGB:51];
        action.font = [UIFont fontWithName:PingFangSC_Regular size:16];
        action.backgroundColor = [UIColor whiteColor];
        
        @weakify(self)
        action.clickBlock = ^{
            
            @strongify(self)
            [self.emotionButton setTitle:@"有人陪" forState:(UIControlStateNormal)];
            [self.emotionButton setTitleColor:[UIColor withRGB:51] forState:(UIControlStateNormal)];
            [self updateParamsWithKey:@"emotion" value:@"有人陪"];
        };
    })
    .LeeAddAction(^(LEEAction *action) {
        
        @strongify(self)
        action.title = @"Ta不在身边";
        action.titleColor = [UIColor withRGB:51];
        action.font = [UIFont fontWithName:PingFangSC_Regular size:16];
        action.backgroundColor = [UIColor whiteColor];
        
        @weakify(self)
        action.clickBlock = ^{
            
            @strongify(self)
            [self.emotionButton setTitle:@"Ta不在身边" forState:(UIControlStateNormal)];
            [self.emotionButton setTitleColor:[UIColor withRGB:51] forState:(UIControlStateNormal)];
            [self updateParamsWithKey:@"emotion" value:@"Ta不在身边"];
        };
    })
    .LeeAddAction(^(LEEAction *action) {
        
        @strongify(self)
        action.title = @"有家有室";
        action.titleColor = [UIColor withRGB:51];
        action.font = [UIFont fontWithName:PingFangSC_Regular size:16];
        action.backgroundColor = [UIColor whiteColor];
        
        @weakify(self)
        action.clickBlock = ^{
            
            @strongify(self)
            [self.emotionButton setTitle:@"有家有室" forState:(UIControlStateNormal)];
            [self.emotionButton setTitleColor:[UIColor withRGB:51] forState:(UIControlStateNormal)];
            [self updateParamsWithKey:@"emotion" value:@"有家有室"];
        };
    })
    .LeeHeaderInsets(UIEdgeInsetsMake(10, 0, 0, 0))
    .LeeActionSheetBottomMargin(0.0f) // 设置底部距离屏幕的边距为0
    .LeeActionSheetBackgroundColor([UIColor whiteColor])
    .LeeCornerRadius(0.0f) // 设置圆角曲率为0
    .LeeConfigMaxWidth(^CGFloat(LEEScreenOrientationType type) {
        
        // 这是最大宽度为屏幕宽度 (横屏和竖屏)
        return CGRectGetWidth([[UIScreen mainScreen] bounds]);
    })
    .LeeShow();
}

- (void)updateParamsWithKey:(NSString *)key value:(id)value {
    NSMutableDictionary *infoDict = [NSMutableDictionary dictionary];
    if (_params) {
        [infoDict addEntriesFromDictionary:_params];
    }
    if (value) {
        infoDict[key] = value;
    }
    self.params = infoDict.mutableCopy;
}

@end
